﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NetTaste;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.基础数据
{
    /// <summary>
    /// 供应商
    /// </summary>
    [ApiController, Route("api/[controller]/[action]")]
    public class TSupplierController : ControllerBase
    {
        private readonly ILogRepository logRepository;
        private readonly ITSupplierRepository supR;

        public TSupplierController(ITSupplierRepository supR, ILogRepository logRepository)
        {
            this.supR = supR;
            this.logRepository = logRepository;
        }
        /// <summary>
        /// 查询供应商
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllTSupplier(string? name, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var res = supR.GetAllTSupplier(name, pageIndex, pageSize, out var totalCount);
            return Ok(BaseResult.Result(res, "200", totalCount));
        }
        /// <summary>
        /// 根据供应商id查看供应商
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTSupplierById(int id)
        {
            if(id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = supR.GetSupplierById(id);
            return Ok(BaseResult.Result(res, "200"));
        }
        /// <summary>
        /// 查询供应商类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTSupplierDic()
        {
            return Ok(BaseResult.Result(supR.GetTSupplierDic().Select(c => new { c.Id, c.ItemName }), "200"));
        }
        /// <summary>
        /// 添加供应商
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddTSupplier(TSupplier supplier)
        {
            if (string.IsNullOrEmpty(supplier.Code) || supplier.Code.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }
            string uname = HttpContext.User.FindFirstValue("UserName");
            string runame = HttpContext.User.FindFirstValue("RealName");
            int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
            supplier.CreateUserId = uid;
            supplier.CreateUserName = runame;
            string message = "";
            var res = supR.AddTSupplier(supplier,out message);
            if (res)
            {
               
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "创建了供应商" + supplier.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！"+message, "500"));
        }
        /// <summary>
        /// 修改供应商
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdTSupplier(TSupplier supplier)
        {
            if (supplier.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(supplier.Code)||supplier.Code.Length<=0)
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }
            string uname = HttpContext.User.FindFirstValue("UserName");
            string runame = HttpContext.User.FindFirstValue("RealName");
            int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
            supplier.ModifyUserId = uid;
            supplier.ModifyUserName = runame;
            string message = "";
            var res = supR.UpdTSupplier(supplier, out message);
            if (res)
            {
               
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 6;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "修改了供应商" + supplier.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！"+message, "500"));
        }
        /// <summary>
        /// 删除供应商
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelSUpplier(int id)
        {
            if(id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = supR.DelTSupplier(id);
            if (res)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = uname + "删除了供应商";
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }
        /// <summary>
        /// 批量删除供应商
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelSUpplierList(int[] idList)
        {
            if (idList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var daRes = true;
            foreach (var id in idList)
            {
                var res = supR.DelTSupplier(id);
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                var logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = res? uname + "删除了供应商":uname+"删除供应商失败";
                //获取前端IP地址
                logg.Ipaddress = ip;
                logg.UserId = rid;
                logg.UserName=uname;
                logg.RealName = runame;
                logg.Status = res ? 1:0;
                logRepository.AddTLod(logg);
                if (!res)
                {
                    daRes = false;
                    break;
                }
            }
            if (daRes)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }
    }
}
